﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Swagger.Filter
{
    /// <summary>
    /// 传入枚举参数描述
    /// </summary>
    public class EnumParameterFilter : IParameterFilter
    {
        public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
        {
            Type type = context.ParameterInfo.ParameterType;
            if (type.IsEnum)
            {
                var array = Enum.GetValues(type);
                List<string> list = new List<string>();
                foreach (var item in array)
                {
                    string name = Enum.GetName(type, item);
                    int value = Convert.ToInt32(item);
                    list.Add($"{name}={value}");
                }
                parameter.Description += "，有效值：" + string.Join(",", list);
            }
        }
    }
}
